package role
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	
	import helper.LoaderHelper;
	
	public class RoleCharacterCreateActionBase extends EventDispatcher
	{
		private var _actionType:String;
		private var _actionName:String;
		private var _actionRepeat:Boolean;
		private var _frames:Array=[];
		private var _assetList:XMLList;
		private var _roleCharacterItemList:Vector.<RoleCharacterItem>=new Vector.<RoleCharacterItem>();
		
		public function RoleCharacterCreateActionBase()
		{
		}
		
		public function get actionName():String
		{
			return _actionName;
		}
		
		public function set actionName(value:String):void
		{
			_actionName = value;
		}
		
		public function get actionType():String
		{
			return _actionType;
		}
		
		public function set actionType(value:String):void
		{
			_actionType = value;
		}
		
		public function get frames():Array
		{
			return _frames;
		}
		
		public function set frames(value:Array):void
		{
			_frames = value;
		}
		
		public function get assetList():XMLList
		{
			return _assetList;
		}
		
		public function set assetList(value:XMLList):void
		{
			_assetList = value;
			
			//资源配置
			for each(var asset:XML in _assetList)
			{
				var itemVo:RoleCharacterAssetVO=new RoleCharacterAssetVO();
				itemVo.type=asset.@type;
				itemVo.name=asset.@name;
				itemVo.path=asset.@path;
				itemVo.rowNumber=asset.@rowNumber;
				itemVo.rowCellNumber=asset.@rowCellNumber;
				itemVo.cellWitdh=asset.@cellWitdh;
				itemVo.cellHeight=asset.@cellHeight;
				itemVo.sortOrder=asset.@sortOrder;
				itemVo.isRepeat=asset.@isRepeat=="true" ? true : false;
				itemVo.repeatNumber=asset.@repeatNumber;
				itemVo.groupType=actionType;
				
				//单个图坐标配置
				var arrPoint:Array=asset.@points.toString().split('|');
				if(arrPoint && arrPoint.length>0)
				{
					for(var i:int=0;i<arrPoint.length;i++)
					{
						var arr:Array=arrPoint[i].toString().split(',');
						itemVo.points.push(new Point(Number(arr[0]), Number(arr[1])));
					}
				}
				
				
				var _loader:LoaderHelper=new LoaderHelper(itemVo.path, 0, loadAssetCallBack, itemVo);
				_loader.loadSync();
			}
		}
		
		private function loadAssetCallBack(data:*):void
		{
			var loader:LoaderHelper=data as LoaderHelper;
			var roleCharacterAssetVO:RoleCharacterAssetVO=loader.data as RoleCharacterAssetVO;
			
			_roleCharacterItemList.push(new RoleCharacterItem(roleCharacterAssetVO.type, roleCharacterAssetVO.groupType, (Object(loader.contentLoaderInfo.content).bitmapData as BitmapData).clone(), roleCharacterAssetVO.rowNumber, roleCharacterAssetVO.rowCellNumber, roleCharacterAssetVO.cellWitdh, roleCharacterAssetVO.cellHeight, roleCharacterAssetVO.sortOrder, roleCharacterAssetVO.points, roleCharacterAssetVO.isRepeat, roleCharacterAssetVO.repeatNumber));
			
			loader.dispose();
			loader=null;
			
			if(_roleCharacterItemList.length>=_assetList.length)
			{//当加载的资源都已加载完成时，则表示这个动作基本数据已装备完毕
				dispatchEvent(new RoleCharacterEvent(RoleCharacterEvent.CHARACTER_ACTIONBASE_ALL_LOAD_COMPLETE, this));
			}
		}

		public function get roleCharacterItemList():Vector.<RoleCharacterItem>
		{
			return _roleCharacterItemList;
		}

		public function set roleCharacterItemList(value:Vector.<RoleCharacterItem>):void
		{
			_roleCharacterItemList = value;
		}

		public function get actionRepeat():Boolean
		{
			return _actionRepeat;
		}

		public function set actionRepeat(value:Boolean):void
		{
			_actionRepeat = value;
		}
	}
}